package lanQiaoBei.数据结构.trie树;
import java.io.*;

/*  trie树模板*/

public class P1 {
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    final static int N = 100010;
    static int[][] son = new int[N][26];//表示每一层的各个字母
    static int[][] pass=new int[N][26];//每个前缀字符被经过多少次
    static int[] cnt = new int[N];
    static int idx;
    static char[]str=new char[N];
    static void scan()throws Exception{
           int n=Integer.parseInt(br.readLine());
           char[]op=new char[2];
           for(int i=0;i<n;i++){
               br.read(op);
               br.read(str);
               System.out.println(str);
               if(op[0]=='I')insert(str);
               else System.out.println(query(str));
           }
    }
    static void insert(char[] str) {
           int p=0;
           for(int i=0;str[i]!='\n';i++){
        	   int u=str[i]-'a';
        	   if(son[p][u]<=0)son[p][u]=++idx;
        	   pass[p][u]++;
        	   p=son[p][u];
           }
           cnt[p]++;
    }

    static int query(char[] str) {
           int p=0;
           for(int i=0;str[i]!='\n';i++){
               int u=str[i]-'a';
               if(son[p][u]<=0)return -1;
               p=son[p][u];
           }
           return cnt[p];
    }
    public static void main(String[]args)throws Exception{
           scan();
    }
}
